package com.lff.mapper;

import com.lff.domain.Bill;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface BillMapper {
    @Select("<script>" +
            "select b.*,p.proName from smbms_bill b,smbms_provider p WHERE b.`providerId`=p.`id`" +
            "<if test=\"providerId!=null and providerId!= 0\"> and b.providerId=#{providerId}</if>" +
            "<if test=\"isPayment!=null and isPayment!= 0\"> and b.isPayment=#{isPayment}</if>" +
            "<if test=\"productName!=null\"> and b.productName like concat(\'%\',#{productName},\'%\')</if>" +
            "</script>")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "billCode", column = "billCode"),
            @Result(property = "productName", column = "productName"),
            @Result(property = "productDesc", column = "productDesc"),
            @Result(property = "productUnit", column = "productUnit"),
            @Result(property = "productCount", column = "productCount"),
            @Result(property = "isPayment", column = "isPayment"),
            @Result(property = "createdBy.id", column = "createdBy"),
            @Result(property = "creationDate", column = "creationDate"),
            @Result(property = "modifyBy.id", column = "modifyBy"),
            @Result(property = "modifyDate", column = "modifyDate"),
            @Result(property = "providerId.proName", column = "proName"),
            @Result(property = "providerId.id", column = "providerId")
    })
    List<Bill> findAll(@Param("providerId") Long queryProviderId, @Param("isPayment") Long queryIsPayment, @Param("productName") String queryProductName);

    @Select("select b.*,p.proName from smbms_bill b,smbms_provider p WHERE b.`providerId`=p.`id` and b.`id`=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "billCode", column = "billCode"),
            @Result(property = "productName", column = "productName"),
            @Result(property = "productDesc", column = "productDesc"),
            @Result(property = "productUnit", column = "productUnit"),
            @Result(property = "productCount", column = "productCount"),
            @Result(property = "isPayment", column = "isPayment"),
            @Result(property = "createdBy.id", column = "createdBy"),
            @Result(property = "creationDate", column = "creationDate"),
            @Result(property = "modifyBy.id", column = "modifyBy"),
            @Result(property = "modifyDate", column = "modifyDate"),
            @Result(property = "providerId.proName", column = "proName"),
            @Result(property = "providerId.id", column = "providerId")
    })
    Bill findById(Long id);


    @Update({"<script>" +
            "update smbms_bill" +
            "<set>" +
            "<if test=\"billCode!=null\">billCode=#{billCode},</if>" +
            "<if test=\"productName!=null\">productName=#{productName},</if>" +
            "<if test=\"productUnit!=null\">productUnit=#{productUnit},</if>" +
            "<if test=\"productCount!=null and productCount!=0\">productCount=#{productCount},</if>" +
            "<if test=\"totalPrice!=totalPrice and totalPrice!=0\">totalPrice=#{totalPrice},</if>" +
            "<if test=\"providerId!=null\">providerId=#{providerId.id},</if>" +
            "<if test=\"isPayment!=null and isPayment!=0\">isPayment=#{isPayment}</if>" +
            "</set>" +
            "where id=#{id}" +
            "</script>"})
    /*
    id	"11"
    billCode	"BILL2016_011"
    productName	"海之蓝"
    productUnit	"瓶"
    productCount	"50.00"
    totalPrice	"10000.00"
    providerId	"1"
    isPayment	"1"
     */
    void updateBill(Bill bill);

    /*
    billCode	"123"
    productName	"123"
    productUnit	"123"
    productCount	"123"
    totalPrice	"123"
    providerId.proName	"10"
    isPayment	"1"
     */
    @Insert("Insert into smbms_bill(billCode,productName,productUnit,productCount,totalPrice,providerId,isPayment)" +
            "values(#{billCode},#{productName},#{productUnit},#{productCount},#{totalPrice},#{providerId.proName},#{isPayment})")
    void addBill(Bill bill);

    @Delete("delete from smbms_bill where id=#{id}")
    Integer deleteById(Long id);
}
